package com.shuoyue.ycgk.net.base;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.net.exception.NetErrorException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;
import retrofit2.HttpException;

/**
 * Created by 江俊超 on 2019/1/22.
 * Version:1.0
 * Description: RxJava 统一订阅
 * ChangeLog:
 */
public abstract class ApiSubscriberNoView<T> implements Observer<T> {
    private BasePresenter presenter;
    private boolean isShowLoading = false;

    public ApiSubscriberNoView(BasePresenter presenter) {
        this.presenter = presenter;
    }


    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onError(Throwable e) {
        Log.e("Subsriber_error", e.getMessage());
        NetErrorException error = null;
        if (e != null) {
            // 对不是自定义抛出的错误进行解析
            if (!(e instanceof NetErrorException)) {
                if (e instanceof UnknownHostException) {
                    error = new NetErrorException(e, NetErrorException.NoConnectError);
                } else if (e instanceof JSONException || e instanceof JsonParseException) {
                    error = new NetErrorException(e, NetErrorException.PARSE_ERROR);
                } else if (e instanceof SocketTimeoutException) {
                    error = new NetErrorException(e, NetErrorException.SocketTimeoutError);
                } else if (e instanceof ConnectException) {
                    error = new NetErrorException(e, NetErrorException.ConnectExceptionError);
                } else if (e instanceof HttpException) {
                    ResponseBody body = ((HttpException) e).response().errorBody();
                    try {
                        String data = body.string();
                        if (!TextUtils.isEmpty(data)) {
                            //如果是统一格式放到这里
                            Gson gson = new Gson();
                            BaseResult baseResult = gson.fromJson(data, BaseResult.class);
                            error = new NetErrorException(baseResult.getMessage(), baseResult.getCode());
                        } else {
                            error = new NetErrorException("发生未知错误", ((HttpException) e).code());
                        }
                        //解析

                    } catch (Exception IOe) {
                        error = new NetErrorException("网络请求错误", ((HttpException) e).code());
                        IOe.printStackTrace();
                    }
                } else {
                    error = new NetErrorException(e, NetErrorException.OTHER);
                }
            } else {
                error = (NetErrorException) e;
            }
        }

        // 回调抽象方法
        onFail(error);
        onFinish();

    }

    @Override
    public void onNext(T t) {
        onFinish();
    }

    /**
     * 回调错误
     */
    protected void onFail(NetErrorException error) {

        onFinish();

    }

    @Override
    public void onComplete() {
        Log.i("Subsriber", "onComplete");
        onFinish();
    }

    public void onFinish() {
        Log.i("Subsriber", "onFinish");

    }
}
